//
//  RNCommonBlockView.m
//  RecallNote
//
//  Created by Z on 14/12/13.
//  Copyright (c) 2014年 Project. All rights reserved.
//

#import "RNCommonBlockView.h"
#import <AVFoundation/AVFoundation.h>

@implementation RNCommonBlockView

- (void) renderView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self  action:@selector(handleTapGesture:)];
    [self addGestureRecognizer:tap];
    
    if (self.soundTag != 8)
    {
        self.musicNote.text = [NSString stringWithFormat:@"%ld",(self.soundTag)%8];
    }
    else
    {
        self.musicNote.text = [NSString stringWithFormat:@"%ld",self.soundTag%8];
    }
}

- (void)handleTapGesture:(id)tap
{
    [self playAudio:self.soundName];
    
    [self playAnimation];
    
    [self.delegate commonBlockDidSelectAtTag:self.soundTag];
}

- (void)playAnimation
{
    self.shadeView.alpha = 0.0f;
    self.shadeView.hidden = NO;
    self.musicIcon.alpha = 0.0f;
    self.musicIcon.hidden = NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.shadeView.alpha = 0.5f;
        self.musicIcon.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.shadeView.alpha = 0.f;
        }completion:^(BOOL finished) {
            self.musicIcon.hidden = YES;
        }];
    }];
}

-(void)playAudio:(NSString*)audioName
{
    static SystemSoundID shake_sound_male_id = 0;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:audioName ofType:@"mp3"];
    if (path) {
        //注册声音到系统
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&shake_sound_male_id);
        AudioServicesPlaySystemSound(shake_sound_male_id);
    }
    
    AudioServicesPlaySystemSound(shake_sound_male_id);
}

@end
